package com.airticket.demo.controller;

import com.airticket.demo.bean.ArrCityFlow;
import com.airticket.demo.bean.ArrCity_AirRoute_AvgRate;
import com.airticket.demo.bean.EachTimeFlow;
import com.airticket.demo.bean.JsonResult;
import com.airticket.demo.bean.result.*;
import com.airticket.demo.bean.search.SearchInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 右视图渲染数据接口
 */
@RestController
public class RightViewController {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询航班延迟率
     * @param searchInfo
     * @return
     */
    @PostMapping("/bar/delayRate")
    public JsonResult<DelayRate> getDelayRate(@RequestBody SearchInfo searchInfo)
    {
        //获取查询关键字
        String keyword=searchInfo.getKeyword();
        //返回数据
        JsonResult jsonResult;
        //设立查询条件，以keyword进行条件查询且按航班延迟率进行降序排列，取前五条数据
        Query query=new Query();
        Sort sort=Sort.by(Sort.Direction.DESC,"avg(rate)");
        Criteria criteria=Criteria.where("arrCityName").is(keyword);
        query.addCriteria(criteria);
        query.with(sort).limit(5);
        query.fields().include("flightNo").include("avg(rate)");
        //查询航线延迟率集合
        List<ArrCity_AirRoute_AvgRate> list=mongoTemplate.find(query,ArrCity_AirRoute_AvgRate.class);
        //查询结果验证
        if(list!=null && list.size()>0)
        {
            List<String> flightNumber=new ArrayList<>();
            List rates=new ArrayList();
            for(ArrCity_AirRoute_AvgRate arrCityAirRouteAvgRate:list)
            {
                flightNumber.add(arrCityAirRouteAvgRate.getFlightNo());
                rates.add((int)(arrCityAirRouteAvgRate.getRate()));
            }
            //返回数据封装
            jsonResult=new JsonResult(true,"",new DelayRate(flightNumber,rates));
        }
        else if(list.size()==0)
        {
            jsonResult=new JsonResult<>(false,"无到达此处的航班");
        }
        else
        {
            jsonResult=new JsonResult<>(false,"查询失败，请重新操作");
        }
        return jsonResult;
    }

    /**
     * 飞往城市占比取前八
     * @return
     */
    @GetMapping("/pie/cityRate")
    public JsonResult<CityLinesList> getCityRate()
    {
        //返回数据
        JsonResult jsonResult;
        //设立查询条件，按航班数进行降序排列，且取前八条数据
        Query query=new Query();
        query.fields().include("arrCityName").include("count");
        Sort sort=Sort.by(Sort.Direction.DESC,"count");
        query.with(sort).limit(8);
        //查询航空公司航线集合
        List<ArrCityFlow> list=mongoTemplate.find(query,ArrCityFlow.class);
        if(list!=null && list.size()>0)
        {
            List<CityLinesCount> cities=new ArrayList<>();
            for(ArrCityFlow arrCityFlow :list)
            {
                cities.add(new CityLinesCount(arrCityFlow.getArrCityName(),arrCityFlow.getCount()));
            }
            //返回数据封装
            jsonResult=new JsonResult(true,"查询成功",new CityLinesList(cities));
        }
        else
        {
            jsonResult=new JsonResult<>(false,"查询失败，请重新操作");
        }
        return jsonResult;
    }

    /**
     * 各航站楼各时段起降数量
     * @return
     */
    @GetMapping("/line/linesOfEachTerminal")
    public JsonResult<LinesEachTimeByBuilding> getDataByBuilding()
    {
        //返回数据
        JsonResult jsonResult;
        //设置查询条件，且按时间进行降序排列
        Sort sort=Sort.by(Sort.Direction.ASC,"ShowTime");
        Query query=new Query();
        query.with(sort);
        query.fields().include("dep_count").include("arr_count");
        //查询各个航站楼数据集合
        List<EachTimeFlow> A=mongoTemplate.find(query, EachTimeFlow.class,"T1_EachTime_Flow");
        List<EachTimeFlow> B=mongoTemplate.find(query, EachTimeFlow.class,"T2_EachTime_Flow");
        //查询结果验证
        if(A!=null && A.size()>0 && B!=null && B.size()>0)
        {
            List land=new ArrayList();
            List takeOff=new ArrayList();
            for(int i=0;i<24;i++)
            {
                //对各航站楼的数据进行整合
                land.add(A.get(i).getArr_count()+B.get(i).getArr_count());
                takeOff.add(A.get(i).getDep_count()+B.get(i).getDep_count());
            }
            //返回数据封装
            jsonResult=new JsonResult(true,"查询成功",new LinesEachTimeByBuilding(land,takeOff));
        }
        else
        {
            jsonResult=new JsonResult<>(false,"查询失败，请重新操作");
        }
        return jsonResult;
    }
}
